package com.kayoxu.ble_printer.utils;

import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import cn.shomes.smart_lock_flutter.utils.ble.FlutterData;


/**
 * Created by KayoXu
 */
public class SharedUtils {

    public static final String pref_ble_device = "pref_ble_device";

    Activity activity;

    public SharedUtils init(Activity activity) {
        this.activity = activity;
        return _instance;
    }


    static SharedUtils _instance;

    public static SharedUtils share() {
        if (null == _instance) {
            _instance = new SharedUtils();
        }
        return _instance;
    }


    public FlutterData.BleDevice getBleDevice() {
        return GsonUtils.gsonToBean((String) getValue(pref_ble_device, ""), FlutterData.BleDevice.class);
    }

    public void saveBleDevice(FlutterData.BleDevice data) {
        setValue(pref_ble_device, GsonUtils.toJson(data));

    }


    /**
     * @return
     */
    private SharedPreferences getConfigSharedPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(activity);
    }

    /**
     * @param KEY   键值对的键
     * @param value 值
     */
    public void setValue(String KEY, Object value) {
        if (null == value) return;

        if (value.getClass() == Double.class) {
            value = ((Double) value).floatValue();
        }
        SharedPreferences sharedPreferences = getConfigSharedPreferences();
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if (value.getClass() == Boolean.class) {
            editor.putBoolean(KEY, (Boolean) value);
        } else if (value.getClass() == Float.class) {
            editor.putFloat(KEY, (Float) value);
        } else if (value.getClass() == Integer.class) {
            editor.putInt(KEY, (Integer) value);
        } else if (value.getClass() == Long.class) {
            editor.putLong(KEY, (Long) value);
        } else if (value.getClass() == String.class) {
            editor.putString(KEY, (String) value);
        }
        editor.apply();
    }

    /**
     * @param KEY      键值对键名
     * @param defValue 默认值
     * @return
     */
    public Object getValue(String KEY, Object defValue) {
        if (defValue.getClass() == Double.class) {
            defValue = ((Double) defValue).floatValue();
        }

        SharedPreferences sharedPreferences = getConfigSharedPreferences();
        if (defValue.getClass() == Boolean.class) {
            return sharedPreferences.getBoolean(KEY, (boolean) defValue);
        } else if (defValue.getClass() == Float.class) {
            return sharedPreferences.getFloat(KEY, (Float) defValue);
        } else if (defValue.getClass() == Integer.class) {
            return sharedPreferences.getInt(KEY, (Integer) defValue);
        } else if (defValue.getClass() == Long.class) {
            return sharedPreferences.getLong(KEY, (Long) defValue);
        } else if (defValue.getClass() == String.class) {
            return sharedPreferences.getString(KEY, (String) defValue);
        }
        return null;
    }

}
